package cn.lopr.im.api.service.impl;

import cn.lopr.im.api.annotation.DataSource;
import cn.lopr.im.api.bean.Article;
import cn.lopr.im.api.bean.ArticleComment;
import cn.lopr.im.api.bean.ArticleOption;
import cn.lopr.im.api.bean.ArticlePlate;
import cn.lopr.im.api.configuration.datasources.DynamicDataSourceEnum;
import cn.lopr.im.api.dao.ArticleCommentMapper;
import cn.lopr.im.api.dao.ArticleMapper;
import cn.lopr.im.api.dao.ArticleOptionMapper;
import cn.lopr.im.api.dao.ArticlePlateMapper;
import cn.lopr.im.api.service.ICommunityDBService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shuangyang7
 * @date 2021/5/22 12:39
 */
@Service
public class CommunityDBServiceImpl implements ICommunityDBService {

    @Autowired
    private ArticleOptionMapper articleOptionMapper;
    @Autowired
    private ArticlePlateMapper articlePlateMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleCommentMapper articleCommentMapper;

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public List<ArticleOption> selectArticleOptionByRecord(ArticleOption articleOption) {
        return articleOptionMapper.selectByRecord(articleOption);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public int addArticleOption(ArticleOption articleOption) {
        return articleOptionMapper.insertSelective(articleOption);
    }

    @Override
    public void updateArticleOptionByRecord(ArticleOption articleOption) {
        articleOptionMapper.updateByPrimaryKeySelective(articleOption);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public PageInfo<Article> selectArticleByPageAndPlateID(String plateId, int page, int size) {
        PageHelper.startPage(page, size);
        List<Article> articles = articleMapper.selectByPlateIdOrUserIdAndPage(plateId, null);
        return new PageInfo<>(articles);
    }

    @Override
    public PageInfo<Article> selectArticleByPageAndUserId(String publishUserId, int page, int size) {
        PageHelper.startPage(page, size);
        List<Article> articles = articleMapper.selectByPlateIdOrUserIdAndPage(null, publishUserId);
        return new PageInfo<>(articles);
    }

    @Override
    public List<Article> selectArticleByRecord(Article article) {
        return articleMapper.selectByRecord(article);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public int updateArticleByRecord(Article article) {
        return articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public int delArticleByID(Article article) {
        return articleMapper.updateByPrimaryKeySelective(article);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public int addArticleByRecord(Article article) {
        return articleMapper.insertSelective(article);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public List<ArticlePlate> selectPlate() {
        return articlePlateMapper.selectByAll();
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public List<ArticleComment> selectArticleCommentBySelective(ArticleComment articleComment) {
        return articleCommentMapper.selectBySelective(articleComment);
    }

    @Override
    public List<ArticleComment> selectArticleCommentByArticleIdAndPage(String articleId, int page, int size) {
        return articleCommentMapper.selectByArticleIdAndPage(articleId, page, size);
    }

    @Override
    public List<ArticleComment> selectArticleCommentByCommentParentId(String ParentId, int page, int size) {
        return articleCommentMapper.selectByCommentParentId(ParentId, page, size);
    }

    @Override
    public Integer countArticleCommentByParentId(String parentId) {
        return articleCommentMapper.countArticleCommentByParentId(parentId);
    }

    @Override
    public int addArticleCommentBySelective(ArticleComment articleComment) {
        return articleCommentMapper.insertSelective(articleComment);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public int updateArticleCommentBySelective(ArticleComment articleComment) {
        return articleCommentMapper.updateByPrimaryKeySelective(articleComment);
    }

    @Override
    @DataSource(DynamicDataSourceEnum.COMMUNITY)
    public int delArticleCommentBySelective(ArticleComment articleComment) {
        return articleCommentMapper.updateByPrimaryKeySelective(articleComment);
    }

    @Override
    public int countArticleLikeByUserId(Integer publishUserId) {
        return articleMapper.countArticleLikeByUserId(publishUserId);
    }

    @Override
    public Integer countArticleVisitByUserId(Integer publishUserId) {
        return articleMapper.countArticleVisitByUserId(publishUserId);
    }


}
